const app = getApp()
import {
    util,
    Toast,
    commonMixin
} from "../../utils/util.js";


Page({
    behaviors: [commonMixin],
    data: {
        name: '', //姓名
        phone: '', //电话
        avatar: '',
        avatarUrl: '',
        address:'',//地址
        sex:'1',
    },
    bindnicknamereviewName(e) {

    },
    onChange(e){
        console.log(e)
    },
    onLoad(options) {
        let userInfo = util.getUserInfo();
        this.setData({
            name: userInfo.name,
            phone: userInfo.phone,
            avatar: userInfo.avatar,
            avatarUrl: userInfo.avatar_file ? userInfo.avatar_file.url : '',
            address: userInfo.address,
            sex: ''+userInfo.sex,
        });
    },
    onShow() {
      let userInfo = util.getUserInfo()
      if (!userInfo.status) {
        app.globalData.loginFlag = true;
        this.navToUrl('/pages/my/index');
      }
    },

    onShareAppMessage() {
       
    },
   
    //提交信息
    submitData() {
        if (this.data.name=='') {
            Toast('请填写姓名！');
            return;
        }
        if (this.data.phone) {
            if (this.data.phone.length != 11) {
                Toast('手机号码格式错误！');
                return;
            }
        }
        wx.showLoading({
            title: '保存中...',
            mask: true
        });
        let userInfo = util.getUserInfo();
        let postData = {
            name: this.data.name,
        }

        //头像
        if (this.data.avatar) {
            postData.avatar = this.data.avatar;
        }

        if (this.data.phone) {
            postData.phone = this.data.phone;
        }

        if (this.data.sex) {
            postData.sex = this.data.sex;
        }
        
        if (this.data.address) {
            postData.address = this.data.address;
        }


        util.http.post("v1/mini/save-userinfo", postData).then((res) => {
            if (res.data.code == 0) {
                Toast('保存成功');
                util.getMysqlUser();
                setTimeout(() => {
                    wx.navigateBack({
                        delta: 1
                    })
                }, 500)
            } else {
                Toast(res.data.message);
            }
            wx.hideLoading();
        }).catch((err) => {
            wx.hideLoading();
            Toast('网络异常！刷新后重试！');
            
        })
    },
    //上传图片
    uploadFile(file) {
        let vm = this;
        wx.showLoading({
            title: '上传中...',
            mask: true
        });
        util.uploadMutiple({
            file: file,
            url: 'v1/upload-file',
            name: 'upload-file',
        }).then((res) => {
            if (res.state) {
                vm.setData({
                    avatarUrl: res.data.data.url,
                    avatar: res.data.data.id
                })
            } else {
                Toast('上传失败！请重新上传！');
            }
            wx.hideLoading();
        });

    },
    //选择头像
    onChooseAvatar(e) {
        const {
            avatarUrl
        } = e.detail;
        this.uploadFile(avatarUrl);
    },
})